unit TemeljnicaReportRDM;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, PoslovanjeSrv_TLB, StdVcl, ReportRDM, FMTBcd, Provider, alProvider,
  DB, SqlExpr;

type
  TrdmTemeljnicaReport = class(TrdmReport, IrdmTemeljnicaReport)
  private
    { Private declarations }
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
    function GetSelectCommandText(DataSet: TSQLDataSet): string; override;
    function GetTableName: string; override;
    procedure RefreshRecord(const Session: ISession;
      const ProviderName: WideString; ID: Integer); safecall;
    procedure Renumber(const Session: ISession; const FieldName,
      SortFieldName: WideString; StartValue, EndValue: Integer); safecall;
    procedure Set_ReadOnly(Param1: WordBool); safecall;
    procedure Set_SQLConnection(Param1: Integer); safecall;
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  TemeljnicaReportFactory: TComponentFactory;

implementation

{$R *.DFM}

constructor TrdmTemeljnicaReport.Create(AOwner: TComponent);
begin
  inherited;
  dspMain.Options := dspMain.Options + [poReadOnly];
end;

function TrdmTemeljnicaReport.GetSelectCommandText(
  DataSet: TSQLDataSet): string;
var
  SQL: TStringList;
begin
  SQL := TStringList.Create;
  try
    if DataSet = dsMain then
    begin
      SQL.Add('select');
        SQL.Add('TEM.*,');
        SQL.Add('STE.NAZIV as SKUPINA_NAZIV,');
        SQL.Add('STE.OZNAKA as SKUPINA_OZNAKA,');
        SQL.Add('TSA.SALDO as SALDO,');
        SQL.Add('TPO.*,');
        SQL.Add('KON.SIFRA as KONTO_SIFRA,');
        SQL.Add('KON.NAZIV as KONTO_NAZIV,');
        SQL.Add('KON.RAZRED_ID as KONTO_RAZRED_ID,');
        SQL.Add('SIF.NAZIV as SIFRANT_NAZIV,');
        SQL.Add('TST.STEVILKA as POSTAVKA_STEVILKA');
      SQL.Add('from TEMELJNICA TEM');
      SQL.Add('inner join SKUPINA_TEMELJNICA STE on (TEM.SKUPINA_ID = STE.ID)');
      SQL.Add('left outer join TEM_GET_SALDO(TEM.ID) TSA on (TEM.ID = TSA.ID)');
      SQL.Add('inner join TEMELJNICA_POSTAVKA TPO on (TEM.ID = TPO.REF_ID)');
      SQL.Add('inner join KONTO KON on (TPO.KONTO_ID = KON.ID)');
      SQL.Add('left outer join SIFRANT SIF on (TPO.SIFRANT_ID = SIF.ID)');
      SQL.Add('left outer join TPO_GET_STEVILKA(TPO.POSTAVKA_ID) TST on (TPO.POSTAVKA_ID = TST.POSTAVKA_ID)');
      SQL.Add('where TEM.REF_ID = :POSLOVNO_LETO_ID');
    end;
  finally
    Result := Trim(SQL.Text);
    SQL.Free;
  end;
end;

function TrdmTemeljnicaReport.GetTableName: string;
begin
  Result := 'TEM';
end;

procedure TrdmTemeljnicaReport.RefreshRecord(const Session: ISession;
  const ProviderName: WideString; ID: Integer);
begin
  inherited;
end;

procedure TrdmTemeljnicaReport.Renumber(const Session: ISession;
  const FieldName, SortFieldName: WideString; StartValue, EndValue: Integer);
begin
  inherited;
end;

procedure TrdmTemeljnicaReport.Set_ReadOnly(Param1: WordBool);
begin
  inherited;
end;

procedure TrdmTemeljnicaReport.Set_SQLConnection(Param1: Integer);
begin
  inherited;
end;

class procedure TrdmTemeljnicaReport.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

initialization
  TemeljnicaReportFactory := TComponentFactory.Create(ComServer, TrdmTemeljnicaReport,
    Class_rdmTemeljnicaReport, ciInternal, tmApartment);
end.
